package com.xiaoyu.base.view

import android.view.View

abstract class OnClickDebounceListener : View.OnClickListener {

    private var lastClickTime: Long = System.currentTimeMillis()
    private var windowDuration: Long = DURATION_NORMAL

    constructor()

    constructor(windowDuration: Long) {
        this.windowDuration = windowDuration
    }

    override fun onClick(v: View) {
        val currentTime = System.currentTimeMillis()
        val canClick = currentTime - lastClickTime > windowDuration
        if (canClick) {
            onClickDebounce(v)
            lastClickTime = currentTime
        }
    }

    abstract fun onClickDebounce(v: View)

    companion object {

        const val DURATION_NORMAL: Long = 300
        const val DURATION_MIDDLE: Long = 500
    }
}
